//
//  FYPictureUtil.m
//  ThePainter
//
//  Created by Sean on 15/9/14.
//  Copyright (c) 2015年 FeiYu. All rights reserved.
//

#import "FYPictureUtil.h"

@implementation FYPictureUtil

+ (UIImage*)clipImageToSquare:(UIImage*)image {
    CGFloat w = image.size.width;
    CGFloat h = image.size.height;
    
    CGFloat newW = 0;
    CGFloat newH = 0;
    CGFloat newX = 0;
    CGFloat newY = 0;
    
    if (w > h) {
        newW = h;
        newH = h;
        newX = newW / 2 - w / 2;
        newY = 0;
    } else {
        newW = w;
        newH = w;
        newX = 0;
        newY = newH / 2 - h / 2;
    }
    
    //剪裁图片
    UIGraphicsBeginImageContext(CGSizeMake(newW, newH));
    [image drawInRect:CGRectMake(newX, newY, w, h)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    
    return newImage;
}

+ (UIImage*)clipImage:(UIImage*)image withSize:(CGSize)size {
    CGFloat w = image.size.width;
    CGFloat h = image.size.height;
    
    CGFloat scale = size.height / size.width;
    
    
    CGFloat newW = 0;
    CGFloat newH = 0;
    CGFloat newX = 0;
    CGFloat newY = 0;
    
    newW = w;
    newH = w * scale;
    newX = newW / 2 - w / 2;
    newY = 0;
    
    //剪裁图片
    UIGraphicsBeginImageContext(CGSizeMake(newW, newH));
    [image drawInRect:CGRectMake(newX, newY, w, h)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    
    return newImage;
}

@end
